/*
 * pwmGen.c
 *
 *  Created on: Sep 5, 2020
 *      Author: Administrator
 */

#include "pwmGen.h"

void setPWMNum(unsigned int num, TIM_HandleTypeDef *htim) {
	if(htim->Instance == htim3.Instance) {
		PWMNum = num;
		stepper_1_flag++;
		HAL_TIM_PWM_Start_IT(htim, TIM_CHANNEL_1);
	} else if (htim->Instance == htim4.Instance) {
		PWMNum2 = num;
		stepper_2_flag++;
		HAL_TIM_PWM_Start_IT(htim, TIM_CHANNEL_4);
	}
}

void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) {

    static unsigned int cnt = 0;

    if(htim->Instance == htim3.Instance) {

        cnt++;

        if(cnt == PWMNum) {

            HAL_TIM_PWM_Stop_IT(&htim3, TIM_CHANNEL_1);

            PWMNum = 0;
            cnt = 0;
            stepper_1_flag++;
        }

    }
    else if (htim->Instance == htim4.Instance) {
    	static unsigned int cnt2 = 0;
        cnt2++;

        if(cnt2 == PWMNum2) {

            HAL_TIM_PWM_Stop_IT(&htim4, TIM_CHANNEL_4);

            PWMNum2 = 0;
            cnt2 = 0;
            stepper_2_flag++;
        }
    }

}
